
clear all
set more off

** Change path to re-run on another computer
cd ""
**



*$$$$$$$$$$$$$$$$$$$$$$$$$
*1) Load data
*$$$$$$$$$$$$$$$$$$$$$$$$$

use "VEPL_FEPI_data.dta", clear

*$$$$$$$$$$$$$$$$$$$$$$$$$
*2) Some descriptives
*$$$$$$$$$$$$$$$$$$$$$$$$$


** Number countries there, coverage:

***VEPL
unique cou if VEPL_MER!=.
unique cou if VEPL_MER!=. & OECD==1
unique cou if VEPL_MER!=. & OECD==0

preserve
bysort cou: egen ctemp=count(VEPL_MER)
drop if ctemp==0
count if VEPL_MER==.
di 1-r(N)/_N
restore
count if VEPL_MER==.
di 1-r(N)/_N


***FEPI

unique cou if FEPI_fw2010!=.
unique cou if FEPI_fw2010!=. & OECD==1
unique cou if FEPI_fw2010!=. & OECD==0  //  + LATVIA

sum FEPI_fw2010 
di r(N)/_N
sum FEPI_allfuels_fw2010
di r(N)/_N

tab flag_VEPL

tab flag_FEPI

tab flag_addprice



***more descriptives

forvalues i=2010/2015{
sum VEPL_MER if year==`i' & sec=="Industry",d 
di r(p90)/r(p10)
di r(p95)/r(p5)
}


*br if (cou=="United States of America" | cou=="Germany") & year==2014
di 755/604 // ger us constr
di 1338/546 // ger us non fer


preserve
keep if sec=="Industry" & year==2014
sort VEPL_MER

di 1703/216 // Italy kaz 2010

di 1157/417 // germany us
di 891/417 // uk us
di 2107/417 // italy us
restore


*Spearmans coefficient of correlation of sector rankings across countries
************************************
preserve
keep VEPL_MER sec cou year
egen gco=group(country)
drop cou
reshape wide VE, i(sec yea) j(gco) 
keep if year==2014
missings dropvars VEPL*, force
spearman VEPL_MER*, stats(rho p)
scalar i=0
scalar j=0
foreach x of varlist VEPL_MER*{
foreach y of varlist VEPL_MER*{
if `x'!=`y'{
spearman `x' `y'
scalar i=i+r(rho)
scalar j=j+1
}
}
}
di i/j
//0.42
restore

*$$$$$$$$$$$$$$$$$$$$$$$$$
*3) Figure 1
*$$$$$$$$$$$$$$$$$$$$$$$$$
set scheme s2manual

*graph with level 1995 and growth rate by country
***********************************************
preserve

*calculate average growth rate
keep if sec=="Industry"
sort cou year
isid cou year
keep cou year  VEPL_MER sec

keep if year==1995|year==2014

tostring year,gen(ye)
replace ye="'"+substr(ye,3,4)

g pos=6 if year==1995
replace pos=12 if year==2014
replace cou="Korea" if cou=="Korea, Republic of"
replace cou="Taiwan" if cou=="Taiwan, Republic of China"
replace cou="USA" if cou=="United States of America"


stripplot VEPL_MER, vertical over(country) msymbol(D) msize(0.7)  mlabel(ye) mlabvposition(pos) mlabsize(*0.6) mcolor(gs7)   xla(, ang(v) labsize(2.3) grid)  /// 
box(bfcolor(gs11) blcolor(white) barw(0.15)  blwidth(none) ) pctile(0) whiskers(lcolor(gs11) lwidth(0.3)) /// 
xtitle("") ytitle("Aggregate industry VEPL in 1995 and 2014 (2010 US$ per toe)",size(2.5)) ylabel(,labsize(2.5)) /// 
plotregion(margin(l-1)) plotregion(margin(r-1)) yscale(range(70 2000))  ylabel(200(200)2000,ang(0)) graphregion(fcolor(white) lcolor(white))
graph export "Graphs/some descriptive statistics/variation in VEPL across countries/01bChart95_14.pdf", replace

restore


*$$$$$$$$$$$$$$$$$$$$$$$$$
*4) Figure 2
*$$$$$$$$$$$$$$$$$$$$$$$$$

preserve
drop if sector=="Industry"
drop if sector=="Industry Unido"
rename VEPL_MER VEPL

encode sector, ge(sector_n)
encode country, ge(country_n)

forvalues i = 1995(1)2015{
	anova VEPL i.sector_n i.country_n if year==`i'
	*ge totvar_`i' = e(mss)
*	ge obs_`i' = e(N)
	*ge v_withincountry_`i' = e(ss_1)
	*ge v_withinsector_`i' = e(ss_2)
	ge sd_`i' = sqrt(e(mss)/(e(N)-1))
	ge p_withincountry_`i' = e(ss_1)/(e(mss)+e(rss))*100
	ge p_withinsector_`i' = e(ss_2)/(e(mss)+e(rss))*100
	}
	
keep if _n==1
keep sd_* p_*
ge id = 1
reshape long sd_ p_withincountry_ p_withinsector_, i(id) j(year) 

lab var p_withincountry_ "Within countries"
lab var p_withinsector_ "Across countries"

ge explained = p_withincountry_  + p_withinsector_
lab var explained "Within countries"

twoway (area    explained p_withinsector_ year), ylabel(0(20)100)  scheme(s1mono) xtitle("All sectors") ytitle("Price dispersion as a percent of the total") title("")
graph export "Graphs/some descriptive statistics/variance decomposition/Figure2a.pdf", replace
restore

preserve
drop if sector=="Industry"
drop if sector=="Industry Unido"
rename VEPL_MER VEPL
*five energy-intensive sectors (iron and steel, chemicals and petrochemicals, cement, pulp and paper, and aluminium
// for energy intensive only
keep if sector=="Iron and steel" | sector=="Chemical and petrochemical" | sector=="Paper, pulp and print" | sector==" Non-metallic minerals"

encode sector, ge(sector_n)
encode country, ge(country_n)

forvalues i = 1995(1)2015{
	anova VEPL i.sector_n i.country_n if year==`i'
	*ge totvar_`i' = e(mss)
*	ge obs_`i' = e(N)
	*ge v_withincountry_`i' = e(ss_1)
	*ge v_withinsector_`i' = e(ss_2)
	ge sd_`i' = sqrt(e(mss)/(e(N)-1))
	ge p_withincountry_`i' = e(ss_1)/(e(mss)+e(rss))*100
	ge p_withinsector_`i' = e(ss_2)/(e(mss)+e(rss))*100
	}
	
keep if _n==1
keep sd_* p_*
ge id = 1
reshape long sd_ p_withincountry_ p_withinsector_, i(id) j(year) 

lab var p_withincountry_ "Within countries"
lab var p_withinsector_ "Across countries"

ge explained = p_withincountry_  + p_withinsector_
lab var explained "Within countries"

twoway (area    explained p_withinsector_ year), ylabel(0(20)100)  scheme(s1mono) xtitle("Energy-intensive") ytitle("Price dispersion as a percent of the total") title("")
graph export "Graphs/some descriptive statistics/variance decomposition/Figure2b.pdf", replace
restore


*$$$$$$$$$$$$$$$$$$$$$$$$$
*5) Figure 6
*$$$$$$$$$$$$$$$$$$$$$$$$$


bysort year: sum VEPL_MER if sec=="Industry",d
di 951/504
di 908/543

levelsof sector, local(sect)
*foreach s of local sect{
foreach s in Industry Machinery Construction{

preserve
keep if sector=="`s'"

drop if VEPL_MER==.
*keep if OECD==1

gen upper=.
gen lower=.
gen iqr=.
gen median=.
gen q25=.
gen q75=.

 
forval y=  1995/2015{
sum VEPL_MER if sector=="`s'" & year==`y',d
replace upper = r(p90) if sector=="`s'" & year==`y'
replace lower = r(p10) if sector=="`s'" & year==`y'
replace median = r(p50) if sector=="`s'" & year==`y'
replace iqr = (r(p90)-r(p10)) if sector=="`s'" & year==`y'
replace q25 = r(p25) if sector=="`s'" & year==`y'
replace q75 = r(p75) if sector=="`s'" & year==`y'
}

label var upper "90% "
label var lower " 10%"
label var iqr "Range VEPL 90% - VEPL10%"
label var median "Median country" 


keep year upper lower median q25 q75
duplicates drop

twoway rarea upper lower  year, color(gs13) lcolor(gs13) || ///
 rarea q75 q25  year, color(gs10) lcolor(gs10) || ///
line   median year, legend( label(1 "10th to 90th percentile") label(2 "25th to 75th percentile")) /*
*/  ytitle("Price in constant 2010 USD/toe", height(5))  ylabel(#9, angle(horizontal)) yscale(r(200 1000)) legend(rows(2)) xlabel(#5, angle(horizontal)) lwidth(medthick) lcolor(black balck black) lpattern(solid solid longdash)  xtitle("`s' VEPL") graphregion(color(white))
graph export "Graphs/some descriptive statistics/Box Plot/VEPL `s' 10 and 90 over time all countries MER.pdf", replace

restore
}




*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*6) Figure 11
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


preserve

keep if sector=="Industry"
keep if cou=="France" |cou=="United Kingdom" | cou=="United States of America" | cou=="Italy" |cou=="Japan" | cou=="Germany"
keep country year VEPL_MER
rename VEPL_MER VEPL
duplicates drop

separate VEPL, by(country)
foreach i of varlist _all {
local a : variable label `i'
local a: subinstr local a "VEPL, country == " ""
label var `i' "`a'"
}

label var VEPL5 "UK"
label var VEPL6 "USA"

line VEPL1 VEPL2 VEPL3 VEPL4 VEPL5 VEPL6 year, /*
*/  ytitle("Price in constant 2010 USD/toe", height(14))  ylabel(#9, angle(horizontal)) yscale(r(200 1800)) legend(rows(2)) xlabel(#5, angle(horizontal)) lcolor(ltblue black midgreen cranberry sand black) lpattern(solid shortdash_dot solid solid solid longdash) graphregion(color(white))
graph export "Graphs/some descriptive statistics/variation in VEPL across time for few countries/few OECD countries _ MER.pdf", replace

restore


*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*7) Figure 12
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

preserve
keep if sector=="Industry"
keep if  cou=="United States of America"  | cou=="Germany" | cou=="China" | cou=="India" | cou=="Brazil" | cou == "Russian Federation" | cou == "South Africa"
keep country year VEPL_MER VEPL_PPP
rename VEPL_MER VEPL

duplicates drop

separate VEPL, by(country)
foreach i of varlist _all {
local a : variable label `i'
local a: subinstr local a "VEPL, country == " ""
label var `i' "`a'"
}

label var VEPL5 "Russia"
label var VEPL6 "S.Africa"
label var VEPL7 "USA"

line VEPL1 VEPL2 VEPL3 VEPL4 VEPL5 VEPL6 VEPL7 year, /*
*/  ytitle("Price in constant 2010 USD/toe", height(5))  ylabel(#9, angle(horizontal)) yscale(r(200 1600)) legend(rows(2)) xlabel(#5, angle(horizontal))  lcolor(ltblue edkblue black midgreen sand orange_red black) lpattern(solid solid shortdash_dot solid solid solid longdash) xtitle("Industry VEPL using MER") graphregion(color(white))
graph export "Graphs/some descriptive statistics/variation in VEPL across time for few countries/Emerging economies countries _ MER.pdf", replace

separate VEPL_PPP, by(country)
foreach i of varlist _all {
local a : variable label `i'
local a: subinstr local a "VEPL_PPP, country == " ""
label var `i' "`a'"
}

label var VEPL_PPP5 "Russia"
label var VEPL_PPP6 "S.Africa"
label var VEPL_PPP7 "USA"

line VEPL_PPP1 VEPL_PPP2 VEPL_PPP3 VEPL_PPP4 VEPL_PPP5 VEPL_PPP6  VEPL_PPP7 year, /*
*/  ytitle("Price in constant 2010 USD/toe", height(5))  ylabel(#9, angle(horizontal)) yscale(r(200 1600)) legend(rows(2)) xlabel(#5, angle(horizontal)) lcolor(ltblue edkblue black midgreen sand red black) lpattern(solid solid shortdash_dot solid solid solid longdash)  xtitle("Industry VEPL using PPP") graphregion(color(white))
graph export "Graphs/some descriptive statistics/variation in VEPL across time for few countries/Emerging economies countries _ PPP.pdf", replace

restore



*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*Figure 3
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

preserve
rename VEPL_MER VEPL

set scheme s2manual
*drop if sector=="Industry"
drop if sector=="Non-specified (industry)"
drop if sector=="Industry Unido"

gen aa=1 if VEPL!=.
bysort cou sector: egen tot=total(aa)
drop if tot!=21

gen cv_s=.
gen cv_c=.

gen mean_s=.
gen sd_s=.

levelsof sector, local(sector) 
levelsof country, local(country)

forval y=  1995/2015{
foreach s of local sector{
sum VEPL if sector=="`s'" & year==`y' 
replace cv_s = r(sd)/r(mean) if sector=="`s'" & year==`y'
replace mean_s = r(mean) if sector=="`s'" & year==`y'
replace sd_s = r(sd) if sector=="`s'" & year==`y'
}
}

forval y=  1995/2015{
foreach c of local country{
sum VEPL if country=="`c'"  & year==`y' & sector!="Industry"
replace cv_c = r(sd)/r(mean) if country=="`c'"  & year==`y' & sector!="Industry"
}
}

label var cv_s "CV for VEPL across countries within sector-year"
label var cv_c "CV for VEPL across sectors within country-year"

graph hbar cv_c if year==2014 & cv_c!=. & sector!="Industry", over(country, label( labsize(vsmall)) sort(1)) ylabel(0[0.1]0.5, labsize(small)) ytitle("CV for VEPL across sectors within country-year") graphregion(color(white)) 
graph export "Graphs/some descriptive statistics/variation in VEPL across sectors/02 CV across sectors all countries.pdf", replace
graph hbar cv_s if year==2014 & cv_s!=. & sector!="Industry", over(sector, label( labsize(vsmall)) sort(1)) ylabel(0[0.1]0.5, labsize(small)) ytitle("CV for VEPL across countries within sector-year") graphregion(color(white)) 
graph export "Graphs/some descriptive statistics/variation in VEPL across countries/02 CV across countries all sectors.pdf", replace

restore 

di 625/604 //ger us construction 2014
di 1334/546 //ger us non ferrous 2014


*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*Figure 13
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

preserve
rename VEPL_MER VEPL

levelsof sector, local(sec)
foreach x in "Construction" "Non-ferrous metals"{
foreach i in  2014{
graph hbar VEPL if sector=="`x'" & year==`i' & VEPL!=., over(country, label( labsize(vsmall)) sort(1)) ytitle("VEPL for `x' in `i'") ylabel(0[200]1400, labsize(small))  graphregion(color(white))
graph export "Graphs/some descriptive statistics/variation in VEPL across countries/`x' `i' bar chart.pdf", replace
}
}
restore

*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*Figure 14
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$



preserve
rename VEPL_MER VEPL

drop if  sector=="Industry" | sector=="Non-specified (industry)" | sector=="Manufacturing"

foreach x in "United States of America" Germany  {
foreach i in  2014{

graph hbar VEPL if country=="`x'" & year==`i' & VEPL!=. & sector!="Industry" & sector!="Non-specified (industry)", over(sector, label( labsize(vsmall)) sort(1)) ytitle("VEPL for `x' in `i'") ylabel(0[200]1200, labsize(small))  graphregion(color(white))
graph export "Graphs/some descriptive statistics/variation in VEPL across sectors/`x' `i' bar chart.pdf", replace
}
}
restore



*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*Figure 8
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

** load data
************************
import excel "Other data/tax data/Taxing Energy Use - Industry ETRs.xlsx", sheet("Industry ETRs") cellrange(A1:F444) firstrow allstring clear

drop in 1

rename A isoalpha3code
rename B sector
rename Tax coal
rename D gas
rename E oil
rename F electricity

destring coal gas oil electricity, replace force

* put in per toe
*put in USD : 1 EUR = 1.3319 USD on 1.3.2012, but 2011 average 1.39, so take 2011 prices instead for consistency later

replace coal=coal*41.868*1.39
replace gas = gas*41.868*1.39
replace oil = oil*41.868*1.39
replace electricity = electricity*41.868*1.39


*rename sectors
replace sector="Chemical and petrochemical" if sector == "CHEMICAL"
replace sector="Construction" if sector == "CONSTRUC"
replace sector="Food and tobacco" if sector == "FOODPRO"
replace sector="Iron and steel" if sector == "IRONSTL"
replace sector="Machinery" if sector == "MACHINE"
replace sector="Mining and quarrying" if sector == "MINING"
replace sector="Non-ferrous metals" if sector == "NONFERR"
replace sector="Non-metallic minerals" if sector == "NONMET"
replace sector="Non-specified (industry)" if sector == "INONSPEC"
replace sector="Paper, pulp and print" if sector == "PAPERPRO"
replace sector="Textile and leather" if sector == "TEXTILES"
replace sector="Transport equipment" if sector == "TRANSEQ"
replace sector="Wood and wood products" if sector == "WOODPRO"



*combine with ER and Deflator data
**********************
*only deflate with 2011, take tax rates from 1.3.2012 as end of year 2011 prices..
 
gen year=2012

merge m:1 isoalpha3code year using "Other data/ER_GDPdeflator.dta"
drop if _m==1 //iceland
*put in LCU

replace coal=coal*exchange_rate
replace gas = gas*exchange_rate
replace oil = oil*exchange_rate
replace electricity = electricity*exchange_rate


*deflate and put in const USD


gen temp = deflator2010/exchange_rate if year == 2010
bysort country: egen scalar=total(temp)

local fuel "coal gas oil electricity"

foreach f of local fuel {
gen `f'_NCU_real=`f'/deflator2010
gen `f'_consUS=`f'_NCU_real*scalar
order `f'_consUS `f'_NCU_real, after(`f')
}

drop scalar temp
sort country year

foreach f of local fuel {
label var `f'_consUS "Constant 2010USD per toe"
}

keep isoalpha3code sector coal_consUS gas_consUS oil_consUS electricity_consUS year country
drop if sector==""


*rename and reshape
rename coal f1
rename gas f2
rename oil f3
rename elec f4

reshape long f, i(country sector year) j(type)

tostring type, replace
replace type="Oil products" if type=="3"
replace type="Coal" if type=="1"
replace type="Natural gas" if type=="2"
replace type="Electricity and heat" if type=="4"

rename f tax_consUSD
label var tax "Constant 2010USD per toe"


**Genergate CVs comparing prices and weights
***********************
preserve

tabstat tax_consUSD if type=="Coal", statistics(cv) by(sector) save

set scheme s2manual

bysort country type: egen m_cou=mean(tax_consUSD)
bysort sector type: egen m_sec=mean(tax_consUSD)
bysort country type: egen sd_cou=sd(tax_consUSD)
bysort sector type: egen sd_sec=sd(tax_consUSD)
gen cv_within_country=sd_cou/m_cou
gen cv_within_sector=sd_sec/m_sec
graph hbar cv_within_country cv_within_sector, over(type)  legend(span label(1 "Within country variation") label(2 "Within sector variation")) ytitle("Coefficient of variation") graphregion(color(white))
graph export "Graphs/some descriptive statistics/sector_lvl/cv_taxes.pdf", replace
restore

clear
